const Service = require("egg").Service;

class CodeService extends Service {
    /**
     * 根据手机号获取验证码
     * @param {string} mobile 手机号
     */
    async findByMobile(mobile) {
        let sql = "select * from code where mobile = ?";
        let params = [mobile];
        const res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    /**
     * 将手机号生成的验证码保存起来
     * @param {string} mobile 手机号
     * @param {string} codeNum 手机号验证码
     */
    async add(mobile, codeNum) {
        let sql = "insert into code (mobile, code) values (?, ?)";
        let params = [mobile, codeNum];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }

    /**
     * 移除手机号对应的验证码记录
     * @param {string} mobile 手机号
     */
    async remove(mobile) {
        let sql = "delete from code where mobile = ?";
        let params = [mobile];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = CodeService;
